* merge county-level wind with county-level worker aggregate data

* merge in worker and wind data together at county level
import sas using "/projects/users/########/Snapshot2022/IntermediateData/county_wrkrpanel_2022.sas7bdat", clear

merge 1:1 ctyfips year using "/projects/users/########/Snapshot2022/IntermediateData/CountyLevelWindDatabase2022.dta"
*drop if _merge ==2	/* don't necessarily drop counties outside our states - want their capacity in the rings */
*replace p_year = 0 if _merge==1
replace c_tcap = 0 if _merge==1		/* c_tcap is the main one we focus on. Highly correlated to c_pcap though */
replace c_tnum = 0 if _merge==1
replace c_tp_cap = 0 if _merge==1
replace c_tp_tnum = 0 if _merge==1
replace c_ptnum = 0 if _merge==1
replace c_pcap = 0 if _merge==1

drop cnty
egen cnty = group(ctyfips)
xtset cnty year

gen cy_tcap = c_tcap if p_year!=.
replace cy_tcap = 0 if p_year ==.

gen stfips = substr(ctyfips,1,2)

gen avg_emp_in = 1 - (total_unemp_wrkrs/tot_pop)
gen avg_emp_earn = (tot_emp_earn/tot_work_pop)
drop tot_pop total_unemp_wrkrs tot_work_pop tot_emp_earn
gen white_emp_in = 1 - (white_unemp_wrkrs/white_pop)
gen white_emp_avgearn = (white_emp_earn/white_work_pop)
drop white_pop white_unemp_wrkrs white_work_pop white_emp_earn
gen black_emp_in = 1 - (black_unemp_wrkrs/black_pop)
gen black_emp_avgearn = (black_emp_earn/black_work_pop)
drop black_pop black_unemp_wrkrs black_work_pop black_emp_earn
gen indnat_emp_in = 1 - (indnat_unemp_wrkrs/indnat_pop)
gen indnat_emp_avgearn = (indnat_emp_earn/indnat_work_pop)
drop indnat_pop indnat_unemp_wrkrs indnat_work_pop indnat_emp_earn
gen hisp_emp_in = 1 - (hisp_unemp_wrkrs/hisp_pop)
gen hisp_emp_avgearn = (hisp_emp_earn/hisp_work_pop)
drop hisp_pop hisp_unemp_wrkrs hisp_work_pop hisp_emp_earn
gen male_emp_in = 1 - (male_unemp_wrkrs/male_pop)
gen male_emp_avgearn = (male_emp_earn/male_work_pop)
drop male_pop male_unemp_wrkrs male_work_pop male_emp_earn
gen female_emp_in = 1 - (female_unemp_wrkrs/female_pop)
gen female_emp_avgearn = (female_emp_earn/female_work_pop)
drop female_pop female_unemp_wrkrs female_work_pop female_emp_earn
gen nohigh_emp_in = 1 - (nohigh_unemp_wrkrs/nohigh_pop)
gen nohigh_emp_avgearn = (nohigh_emp_earn/nohigh_work_pop)
drop nohigh_pop nohigh_unemp_wrkrs nohigh_work_pop nohigh_emp_earn
gen highsch_emp_in = 1 - (highsch_unemp_wrkrs/highsch_pop)
gen highsch_emp_avgearn = (highsch_emp_earn/highsch_work_pop)
drop highsch_pop highsch_unemp_wrkrs highsch_work_pop highsch_emp_earn
gen somecoll_emp_in = 1 - (somecoll_unemp_wrkrs/somecoll_pop)
gen somecoll_emp_avgearn = (somecoll_emp_earn/somecoll_work_pop)
drop somecoll_pop somecoll_unemp_wrkrs somecoll_work_pop somecoll_emp_earn
gen college_emp_in = 1 - (college_unemp_wrkrs/college_pop)
gen college_emp_avgearn = (college_emp_earn/college_work_pop)
drop college_pop college_unemp_wrkrs college_work_pop college_emp_earn

save "/projects/users/########/Snapshot2022/IntermediateData/county_windworker_panel2022.dta", replace

